package de.szut.util;

import java.util.ArrayList;

/**
 * Structure that contains printable content and allows for easy management of printable content
 * @author Marc Huisinga
 *
 */
public class PrintableContent implements Resettable, Printable {
	private ArrayList<String> values;
	private ArrayList<String> descriptions;
	
	@Override
	public void reset() {
		values = new ArrayList<String>();
		descriptions = new ArrayList<String>();
	}
	
	/**
	 * Adds a printable value to PrintableContent
	 * @param desc - Description of the value
	 * @param value - Value
	 * @return PrintableContent was changed
	 */
	public boolean add(String desc, String value) {
		boolean contained = false;
		for (String currentDesc : descriptions) {
			if (currentDesc.equals(desc)) {
				for (String currentValue : values) {
					if (currentValue.equals(value)) {
						contained = true;
						break;
					}
				}
			}
		}
		if (!contained) {
			values.add(value);
			descriptions.add(desc);
		}
		
		return contained;
	}
	
	/**
	 * Removes the specified value with the specified description from PrintableContent.
	 * Value is only removed if the value and the description are contained within PrintableContent.
	 * @param desc - Description of the value to remove
	 * @param value - Value to remove
	 * @return PrintableContent was changed as result to this operation
	 */
	public boolean remove(String desc, String value) {
		int foundAt = 0;
		for (String currentDesc : descriptions) {
			if (currentDesc.equals(desc) && values.get(foundAt).equals(value)) {
				descriptions.remove(foundAt);
				values.remove(foundAt);
				return true;
			}
			foundAt ++;
		}
		return false;
	}
	
	public PrintableContent() {
		reset();
	}

	@Override
	public void printContent() {
		int index = 0;
		for (String currentDesc : descriptions) {
			System.out.println(currentDesc + ": " + values.get(index));
			index ++;
		}
	}
}
